| Conditions | 4 |
| Total Lines | 60 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { Inject } from '@nestjs/common'; |
||
| 34 | |||
| 35 | public async execute(command: AcceptLeaveRequestCommand): Promise<string> { |
||
| 36 | const { moderator, moderationComment, id } = command; |
||
| 37 | |||
| 38 | const leaveRequest = await this.leaveRequestRepository.findOneById(id); |
||
| 39 | if (!leaveRequest) { |
||
| 40 | throw new LeaveRequestNotFoundException(); |
||
| 41 | } |
||
| 42 | |||
| 43 | if ( |
||
| 44 | false === |
||
| 45 | this.canLeaveRequestBeModerated.isSatisfiedBy(leaveRequest, moderator) |
||
| 46 | ) { |
||
| 47 | throw new LeaveRequestCantBeModeratedException(); |
||
| 48 | } |
||
| 49 | |||
| 50 | if ( |
||
| 51 | true === |
||
| 52 | (await this.doesLeaveExistForPeriod.isSatisfiedBy( |
||
| 53 | leaveRequest.getUser(), |
||
| 54 | leaveRequest.getStartDate(), |
||
| 55 | leaveRequest.getEndDate() |
||
| 56 | )) |
||
| 57 | ) { |
||
| 58 | throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
||
| 59 | } |
||
| 60 | |||
| 61 | leaveRequest.accept( |
||
| 62 | moderator, |
||
| 63 | this.dateUtils.getCurrentDateToISOString(), |
||
| 64 | moderationComment |
||
| 65 | ); |
||
| 66 | |||
| 67 | await this.leaveRequestRepository.save(leaveRequest); |
||
| 68 | this.eventBus.publish(new AcceptedLeaveRequestEvent(leaveRequest)); |
||
| 69 | |||
| 70 | this.commandBus.execute( |
||
| 71 | new CreateNotificationCommand( |
||
| 72 | NotificationType.REACTION, |
||
| 73 | this.translator.translate( |
||
| 74 | 'leave-requests-approve-notification-emoji-name' |
||
| 75 | ), |
||
| 76 | leaveRequest |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | |||
| 80 | this.commandBus.execute( |
||
| 81 | new CreateNotificationCommand( |
||
| 82 | NotificationType.COMMENT, |
||
| 83 | this.translator.translate( |
||
| 84 | 'leave-requests-approve-notification-message', |
||
| 85 | { |
||
| 86 | moderatorFirstName: moderator.getFirstName() |
||
| 87 | } |
||
| 88 | ), |
||
| 89 | leaveRequest |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | |||
| 93 | return leaveRequest.getId(); |
||
| 94 | } |
||
| 96 |